<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/departmenthead/DepartmentHeadAction.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class IndicatorValueListAction extends DepartmentHeadAction {
	protected function execute(Response $response) {
		// РїРѕР»СѓС‡РµРЅРёРµ С‚РёРїРѕРІ СЃС‚СЂСѓРєС‚СѓСЂРЅС‹С… РїРѕРґСЂР°Р·РґРµР»РµРЅРёР№ 
		//  Рё Р°РєС‚РёРІРЅРѕРіРѕ С‚РёРїР°, РІС‹Р±СЂР°РЅРЅРѕРіРѕ РїРѕР»СЊР·РѕРІР°С‚РµР»РµРј
		//$service = getService('DepartmentTypeService');
		//$types = $service->getAll();
		//$response->setParameter('types', $types);
		//if(isset($_POST['identity'])) {
		//	$type = $service->read($_POST['identity']);
		//} else {
		//	$type = $types[0];
		//}
		//$response->setParameter('type', $type);
		
		// РѕРїСЂРµРґРµР»РµРЅРёРµ РіРѕРґР°, РґР»СЏ РєРѕС‚РѕСЂРѕРіРѕ РІС‹РїРѕР»РЅСЏРµС‚СЃСЏ
		//  РїСЂРѕСЃРјРѕС‚СЂ С†РµР»РµРІС‹С… РїРѕРєР°Р·Р°С‚РµР»РµР№ 
		if(isset($_POST['year'])) {
			$year = $_POST['year'];
		} elseif (isset($_SESSION['year'])) {
			$year = unserialize($_SESSION['year']);
		} else {
			$year = date("Y");
		}
		$_SESSION['year'] = serialize($year);
		
		// РћРїСЂРµРґРµР»РµРЅРёРµ СЃС‚СЂСѓРєС‚СѓСЂРЅС‹С… РїРѕРґСЂР°Р·РґРµР»РµРЅРёР№, РєРѕС‚РѕСЂС‹РјРё СЂСѓРєРѕРІРѕРґРёС‚ С‚РµРєСѓС‰РёР№ user
		$serviceFactory = new ServiceFactory();
		$service = $serviceFactory->getService('DepartmentService');
		$departments = $service->findByHead(unserialize($_SESSION['user']));
		
		// РџРѕР»СѓС‡РµРЅРёРµ responsibilities Сѓ РєРѕС‚РѕСЂС‹С… is_monitoring=true
		$service = $serviceFactory->getService('ResponsibilityService');
		$monitoringRespsTemp = array();
		foreach ($departments as $d){
			$monitoringRespsTemp[]=$service->getByYearDepAndIsMonitoring($d->getIdentity(),$year);
		}
		$monitoringResps = array();
		foreach ($monitoringRespsTemp as $mrt){
			$monitoringResps=array_merge($monitoringResps,$mrt);
		}
		
		// Р”Р»СЏ РєР°Р¶РґРѕР№ РЅРёС… РЅР°С…РѕРґРёРј responsibility СЃ С‚Р°РєРёРј Р¶Рµ id_indicator
		$responsibilitiesTemp = array();
		foreach ($monitoringResps as $monitoringResp){
			$responsibility = $service->getByIdIndicatorAndYear($monitoringResp->getIndicator()->getIdentity(), $year);
			$responsibilitiesTemp=array_merge($responsibilitiesTemp,$responsibility);	
		}
		
		// РЈРїРѕСЂСЏРґРѕС‡РёРІР°РµРј responsibilities СЃРЅР°С‡Р°Р»Р° РїРѕ department_type, Р° РїРѕС‚РѕРј РїРѕ department
		$serviceIndicatorV = $serviceFactory->getService('IndicatorValueService');
		$indicatorValues = array();
		$responsibilitiesTemp=array_merge($responsibilitiesTemp,$monitoringResps);
		$responsibilities = array();
		foreach ($responsibilitiesTemp as $rTemp){
			$flag = false;
			foreach ($departments as $d){
				if ($rTemp->getDepartment()->getIdentity()==$d->getIdentity()) {
					$flag=true;
				}
			}
			if (!$flag) {
			
			if (array_key_exists($rTemp->getDepartment()->getType()->getName(), $responsibilities)) {
				if (!array_key_exists($rTemp->getDepartment()->getName(), $responsibilities[$rTemp->getDepartment()->getType()->getName()])) {
					$responsibilities[$rTemp->getDepartment()->getType()->getName()][$rTemp->getDepartment()->getName()]=array();
				}
			} else {
				$responsibilities[$rTemp->getDepartment()->getType()->getName()]=array();
				$responsibilities[$rTemp->getDepartment()->getType()->getName()][$rTemp->getDepartment()->getName()]=array();
			}
			$responsibilities[$rTemp->getDepartment()->getType()->getName()][$rTemp->getDepartment()->getName()][]=$rTemp;
			$indicatorValuesTemp = $serviceIndicatorV->getByResponsibility($rTemp->getIdentity());
			$indicatorValues[$rTemp->getIdentity()] = array();
			foreach ($indicatorValuesTemp as $indicatorValue) {
				$indicatorValues[$rTemp->getIdentity()][$indicatorValue->getQuarter()] = $indicatorValue;
			}
			}	
		}
		$response->setParameter('responsibilities', $responsibilities);
		$response->setParameter('indicatorValues', $indicatorValues);
		$response->setParameter('title', 'РњРѕРЅРёС‚РѕСЂРёРЅРі С†РµР»РµРІС‹С… РїРѕРєР°Р·Р°С‚РµР»РµР№');
	}
}
?>